package com.pan.admin.generator.strategy;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.io.IOUtils;
import com.pan.admin.common.constant.Constants;
import com.pan.admin.generator.config.ApiGenFieldTemplateConfig;
import com.pan.admin.generator.constant.ApiGenApiChannelDataExchangeRespFieldEnum;
import com.pan.admin.generator.constant.ApiGenFieldEnum;
import com.pan.admin.generator.constant.ApiGenPlatParseDataExchangeFieldEnum;
import com.pan.admin.generator.entity.ApiGenField;
import com.pan.admin.generator.entity.ApiGenTemplate;
import com.pan.admin.generator.util.ApiGenUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 *  API生成器-渠道
 *
 *  @author Pan
 */
@Service
public class ApiGenApiChannel implements ApiGenStrategy {

    @Override
    public Map<String, String> viewTemplate(ApiGenTemplate apiGenTemplate, VelocityContext vmContext) {
        String customName = apiGenTemplate.getCustomName();

        //  获取模板对象
        Map<String, String> templateMap = this.listTemplate(customName);
        IteratorUtils.entrySet(templateMap, ((template, downloadPath) -> {
            //  渲染模板
            StringWriter sw = null;
            try {
                sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(vmContext, sw);
            } finally {
                IOUtils.close(sw);
            }
            templateMap.put(template, sw.toString());
        }));
        return templateMap;
    }

    @Override
    public void parseGenTemplateContext(ApiGenTemplate apiGenTemplate, VelocityContext vmContext) {

    }

    @Override
    public Map<String, String> listTemplate(String customName) {
        return Map.of();
    }

    @Override
    public String getFileName(String vmTemplateName, String customName) {
        return "";
    }

    @Override
    public List<ApiGenField> listFieldTemplate(ApiGenFieldTemplateConfig apiGenFieldTemplateConfig) {
        List<ApiGenField> template = CollUtils.newArrayList();

        //  解析撞库响应模板
        if (apiGenFieldTemplateConfig.getHasParseDataExchange()) {
            ApiGenApiChannelDataExchangeRespFieldEnum[] ApiGenApiChannelDataExchangeRespFieldEnums = ApiGenApiChannelDataExchangeRespFieldEnum.values();
            for (ApiGenApiChannelDataExchangeRespFieldEnum apiGenApiChannelDataExchangeRespFieldEnum: ApiGenApiChannelDataExchangeRespFieldEnums) {
                ApiGenField apiGenField = ApiGenField.ofTemplate(apiGenApiChannelDataExchangeRespFieldEnum);
                template.add(apiGenField);
            }
            return template;
        }

        //  获取所有字段模板
        ApiGenFieldEnum[] values = ApiGenFieldEnum.values();
        for (ApiGenFieldEnum apiGenFieldEnum: values) {
            template.add(ApiGenField.ofTemplate(apiGenFieldEnum));
        }
        return template;
    }
}
